import socket
import time
from cmd import *


import thread

class Client(SocketAgent):
    def __init__(self, s=None):
        SocketAgent.__init__(self)
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) if s is None else s
        self.nickname = 'Falldog'
        
        self.isChating = False
        self.isLogin = False

    def connect(self, host, port):
        r = self.socket.connect((host, port))
        print r
        
    def sendcmd(self, cmd):
        msg = EncodeCmd2Server(cmd)
        self.sendmsg(msg)
        
    def recvcmd(self):
        return DecodeCmdFromServer( self.recvline(includeNewLine=True) )


def StdInput(client):
    while True:
        data = raw_input()
        client.sendmsg(data+'\n')


c = Client()
c.connect('localhost', 777)
c.sendcmd(CMD_C_LOGIN)
print 'Login Status ... ', c.recvcmd()
#c.sendcmd(CMD_C_NICKNAME)
c.sendcmd(CMD_C_WANTCHAT)
print 'Chat Status ...', c.recvcmd()

thread.start_new_thread(StdInput, (c,))

print '==============================='
print 'connected on server, plase talk with your partner ...'
while True:
    msg = c.recvline()
    print '@ReceiveMsg:', msg
    
    time.sleep(1)
    